package com.lemon.smartsafetynew.ui.activity.device;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.device.DeviceOverviewVo;
import com.lemon.smartsafetynew.net.services.DeviceServices;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Andrew on 2017/9/15.
 */

public class DeviceHomeActivity extends SwipeBackActivity {

    @Bind(R.id.using_count_tv)
    TextView usingCountTv;
    @Bind(R.id.maintain_count_tv)
    TextView maintainCountTv;
    @Bind(R.id.repair_count_tv)
    TextView repairCountTv;
    @Bind(R.id.scrap_count_tv)
    TextView scrapCountTv;
    @Bind(R.id.device_num_count)
    TextView deviceNumCount;
    @Bind(R.id.device_list_layout)
    LinearLayout deviceListLayout;

    @Override
    protected void initTitle() {
        this.setBarTitle("设备管理");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(DeviceHomeActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_device_home;
    }

    @OnClick(R.id.device_list_layout)
    public void onViewClicked() {
        UIHelper.showActivity(DeviceHomeActivity.this, DeviceListActivity.class);
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        setIconEnable(menu, true);
//        getMenuInflater().inflate(R.menu.menu_add, menu);
//        return true;
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        switch (item.getItemId()) {
//            case R.id.action_add: {
//                //TODO
//                break;
//            }
//        }
//        return super.onOptionsItemSelected(item);
//    }

    @Override
    protected void initViews() {
        super.initViews();

        getDeviceManagerOverView();
    }

    /**
     * 获取设备管理首页数据
     */
    private void getDeviceManagerOverView() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", AccountUtil.getCompanyId());

        DeviceServices
                .init()
                .getDeviceManger(params);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {
        if (KeyConstants.URL_GET_DEVICE_MANAGER.equals(event.getTag())) {                            //设备首页
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    List<DeviceOverviewVo> deviceOverviewVos = (List<DeviceOverviewVo>) event.getObj();
                    if (deviceOverviewVos != null && deviceOverviewVos.size() > 0) {
                        initCommon(deviceOverviewVos.get(0));
                    }

                } else if (Constants.Result.ERROR == event.getEventResult()) {

                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });

                }
            }
        }
    }

    /**
     * 显示设备概览
     */
    private void initCommon(DeviceOverviewVo deviceOverviewVo) {
        usingCountTv.setText(""+CommonUtil.avoidNullString2Zero(deviceOverviewVo.getEnable_num()));
        maintainCountTv.setText(""+CommonUtil.avoidNullString2Zero(deviceOverviewVo.getMaintain_num()));
        repairCountTv.setText(""+CommonUtil.avoidNullString2Zero(deviceOverviewVo.getService_num()));
        scrapCountTv.setText(""+CommonUtil.avoidNullString2Zero(deviceOverviewVo.getScrap_num()));
        deviceNumCount.setText(""+CommonUtil.avoidNullString2Zero(deviceOverviewVo.getTotal_num()));
    }

}
